<?php

class Application_Model_ProveedorConsumo extends Sgit_Model
{  
    protected $_name = 'consumo_datacenter';
    protected $_primary = 'id';
    
    public function getConsumo($id)
    {
        return $this->getConsumos(array(),$id);
    }
    
    
    public function getConsumos($values = array(), $id = null)
    {
        
        $select = $this->getAdapter()->select();


        $select->from($this->_name);
        $select->joinLeft('proveedores', "proveedores.id = {$this->_name}.id_proveedor", array('proveedor' => 'nombre',  'id_zona'));
        $select->where('proveedores.activo = ?',1);


        if (!is_null($id))
        {
            $select->where("{$this->_name}.id= ?", $id);
        } else
        {
            if (is_numeric($values['id_zona']))
                $select->where("proveedores.id_zona= ?", $values['id_zona']);
            if (is_numeric($values['id_proveedor']))
                $select->where("{$this->_name}.id_proveedor= ?", $values['id_proveedor']);
            if (is_numeric($values['anno']))
                $select->where("{$this->_name}.anno= ?", $values['anno']);
            if (is_numeric($values['mes']))
                $select->where("{$this->_name}.mes= ?", $values['mes']);
  
            $select->order(array('anno desc','mes desc'));
        }

        if (is_numeric($values['limit']))
            $select->limit($values['limit']);
  
  

        $rows = $this->getAdapter()->fetchAll($select);
        
        
        
        
        $f=new Zend_Date();
        $f->setDay(1);
        foreach($rows as &$v)
        {
            $f->setYear($v['anno'])->setMonth($v['mes']);
            $v['potencia']=Sgit_Util_Potencia::run($v['consumo'],$f);
            $v['fecha']=$f->addMonth(1)->get('MMM-yyyy');
            $v['precio']=$v['importe']/$v['consumo'];
            
        }

        
        
        if (is_null($id))
            return $rows;
        else
            return $rows[0];
        

    }
    
    public function getConsumosGroup($values = array(), $id = null)
    {
        
        $select = $this->getAdapter()->select();


        $select->from(array($this->_name),array('anno','mes','consumo'=>'SUM(consumo)','importe'=>'SUM(importe)','potencia_max'=>'SUM(potencia_max)'));
        $select->joinLeft('proveedores', "proveedores.id = {$this->_name}.id_proveedor", array('id_zona'));
        $select->where('proveedores.activo = ?',1);


        if (!is_null($id))
        {
            $select->where("{$this->_name}.id= ?", $id);
        } else
        {
            if (is_numeric($values['id_zona']))
                $select->where("proveedores.id_zona= ?", $values['id_zona']);
            if (is_numeric($values['id_proveedor']))
                $select->where("{$this->_name}.id_proveedor= ?", $values['id_proveedor']);
            if (is_numeric($values['anno']))
                $select->where("{$this->_name}.anno= ?", $values['anno']);
            if (is_numeric($values['mes']))
                $select->where("{$this->_name}.mes= ?", $values['mes']);
  
            $select->order(array('anno desc','mes desc'));
        }

        $select->group(array('id_zona','anno','mes'));
        
        if (is_numeric($values['limit']))
            $select->limit($values['limit']);


        $rows = $this->getAdapter()->fetchAll($select);


        $f=new Zend_Date();
        $f->setDay(1);
        foreach($rows as &$v)
        {
            $f->setYear($v['anno'])->setMonth($v['mes']);
            $v['potencia']=Sgit_Util_Potencia::run($v['consumo'],$f);
            $v['fecha']=$f->addMonth(1)->get('MMM-yyyy');
            $v['precio']=$v['importe']/$v['consumo'];
            
        }
        
        if (is_null($id))
            return $rows;
        else
            return $rows[0];
        

    }
    
    
    public function precio_estimado($id_zona,$limit=18){
        
        $f=new Zend_Date();
        $f->setDay(10)->subMonth($limit);


        $select = $this->getAdapter()->select()
        ->from(array($this->_name),array('mes','consumo'=>'ROUND(AVG( importe / consumo),4)'))
        ->joinLeft('proveedores', "proveedores.id = {$this->_name}.id_proveedor", array('id_zona'))
        ->where('proveedores.activo = ?',1)
        ->where("DATE_FORMAT(CONCAT(anno,'-',mes,'-10'), '%Y-%m-%d') >= ?", $f->get('yyyy-MM-dd'))
        ->group('mes');
        
        if (is_numeric($id_zona))
            $select->where("proveedores.id_zona= ?",$id_zona);

        $data = $this->db->fetchPairs($select);
        return $data;

    }

    
    
    
}  




